<template>
  <div class="app-container" style="height: 450px">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-check"
          size="small"
          :disabled="orderList.length===0"
          @click="handleOrder"
        >下单
        </el-button>
      </el-col>

    </el-row>
    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange" height="450px">
      <el-table-column label="菜品名称" align="left" prop="dishesName"/>
      <el-table-column label="菜品类型" align="left" prop="dishesType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dish_type" :value="scope.row.dishesType"/>
        </template>
      </el-table-column>
      <el-table-column label="单价" align="center" prop="dishesPrice"/>
      <el-table-column label="数量" align="center" prop="dishesType">
        <template slot-scope="scope">
          <el-input-number v-model="scope.row.num" :min="0" size="small"
                           @change="handleNum(scope.row)"></el-input-number>
        </template>
      </el-table-column>
    </el-table>

    <div style="padding: 20px">
      <el-row :gutter="20">
        <el-col :span="6" v-for="(item,index) in orderList">
          <span>{{item.dishesName}} / {{ item.num}} 份 / {{item.totalPrice}} 元</span>
        </el-col>
      </el-row>
      <div style="text-align: right;padding: 20px">
        <h3>总价： <span style="color: #bd0b0b">{{totalPrice}}</span></h3>
      </div>

    </div>

    <!--<pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />-->

    <!-- 添加或修改菜单信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="房间号" prop="roomNum">
          <el-input v-model="form.roomNum" placeholder="请输入房间号"/>
        </el-form-item>
        <el-form-item label="手机号" prop="photo">
          <el-input v-model="form.photo" placeholder="请输入手机号"/>
        </el-form-item>
        <el-form-item label="总价" prop="totalPrice">
          <el-input v-model="form.totalPrice" disabled/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="详情">
          <el-row :gutter="20" style="width: 100%">
            <el-col :span="12" v-for="(item,index) in orderList">
              <span>{{item.dishesName}} / {{ item.num}} 份 / {{item.totalPrice}} 元</span>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listInfo, getInfo, delInfo, addInfo, updateInfo } from '@/api/hotel/dishes'
  import { addOrder } from '@/api/hotel/dishOrder'

  export default {
    dicts: ['dish_type'],
    name: 'manage',
    data() {
      // 自定义验证规则
      let checkNumber = (rule, value, callback) => {
        if ( !value ) {
          return callback (new Error ('单价不能为空'))
        }
        setTimeout (() => {
          if ( !Number.isInteger (value) ) {
            callback (new Error ('请输入数字值'))
          } else {
            callback ()
          }
        }, 1000)
      }
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 菜单信息表格数据
        infoList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 100,
          dishesName: null,
          dishesType: null,
          dishesPrice: null,
          state: null,
          provideScope: null
        },
        // 表单参数
        form: {},
        totalPrice: 0,
        // 表单校验
        rules: {
          photo: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          roomNum: [
            { required: true, message: '房间号不能为空', trigger: 'blur' }
          ]
        },
        orderList: [],
        orderNum: []
      }
    },
    created() {
      this.getList ()
    },
    methods: {

      handleNum(rowData) {
        rowData.totalPrice = rowData.num * rowData.dishesPrice
        let flag = this.orderNum.findIndex (item => item === rowData.id)
        if ( flag === -1 ) {
          this.orderList.push (rowData)
          this.orderNum.push (rowData.id)
        } else {
          if ( rowData.num === 0 ) {
            this.orderList.splice (this.orderList.indexOf (rowData), 1)
            this.orderNum.splice (this.orderNum.indexOf (rowData.id), 1)
          }
        }
        this.totalPrice = 0
        this.orderList.forEach (item => {
          this.totalPrice += item.totalPrice

        })
      },
      /** 查询菜单信息列表 */
      getList() {
        this.queryParams.state = '在售'
        this.loading = true
        listInfo (this.queryParams).then (response => {
          this.infoList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset ()
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          totalPrice: null,
          photo: null,
          roomNum: null,
          state: '0',
          dishesInfo:[]
        }
        this.resetForm ('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList ()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm ('queryForm')
        this.handleQuery ()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map (item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset ()
        this.open = true
        this.title = '添加菜单信息'
      },
      /** 修改按钮操作 */
      handleOrder() {
        this.reset ()
        this.form.dishesInfo = this.orderList
        this.form.totalPrice = this.totalPrice
        this.open = true
        this.title = '下单信息'

      },
      /** 提交按钮 */
      submitForm() {
        this.$refs[ 'form' ].validate (valid => {
          if ( valid ) {
            addOrder (this.form).then (response => {
                this.$modal.msgSuccess ('下单成功')
                this.open = false
                this.getList ()
              })
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        this.$modal.confirm ('是否确认删除菜单信息编号为"' + ids + '"的数据项？').then (function() {
          return delInfo (ids)
        }).then (() => {
          this.getList ()
          this.$modal.msgSuccess ('删除成功')
        }).catch (() => {
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download ('system/info/export', {
          ... this.queryParams
        }, `info_${new Date ().getTime ()}.xlsx`)
      }
    }
  }
</script>
